package com.example.demo.service;

import com.example.demo.model.College;
import com.example.demo.repository.CollegeRepository;
import com.example.demo.repository.MajorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class CollegeService {
    @Autowired
    private CollegeRepository collegeRepository;
    
    // 注入其他Repository用于检查关联数据
    @Autowired
    private MajorRepository majorRepository;

    public Page<College> getAllColleges(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<College> collegePage = collegeRepository.findAll(pageable);
        // 为每个学院填充关联信息
        return collegePage.map(this::populateCollegeInfo);
    }
    
    // 添加搜索学院的方法
    public Page<College> searchColleges(String name, College.Status status, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        
        // 如果同时提供了名称和状态，则使用组合搜索
        if (name != null && !name.isEmpty() && status != null) {
            return collegeRepository.findByNameContainingAndStatus(name, status, pageable);
        }
        // 如果只提供了名称，则按名称搜索
        else if (name != null && !name.isEmpty()) {
            return collegeRepository.findByNameContaining(name, pageable);
        }
        // 如果只提供了状态，则按状态搜索
        else if (status != null) {
            return collegeRepository.findByStatus(status, pageable);
        }
        // 如果都没有提供，则返回所有学院
        else {
            Page<College> collegePage = collegeRepository.findAll(pageable);
            // 为每个学院填充关联信息
            return collegePage.map(this::populateCollegeInfo);
        }
    }
    
    private College populateCollegeInfo(College college) {
        // 填充专业数量信息
        if (college.getId() != null) {
            int majorCount = majorRepository.findByCollegeId(college.getId()).size();
            college.setMajorCount(majorCount);
        }
        
        return college;
    }

    public Optional<College> getCollegeById(Long id) {
        Optional<College> collegeOpt = collegeRepository.findById(id);
        if (collegeOpt.isPresent()) {
            College college = collegeOpt.get();
            populateCollegeInfo(college);
            return Optional.of(college);
        }
        return collegeOpt;
    }

    public College saveCollege(College college) {
        return collegeRepository.save(college);
    }

    public void deleteCollege(Long id) {
        collegeRepository.deleteById(id);
    }

    public Optional<College> getCollegeByCode(String code) {
        Optional<College> collegeOpt = collegeRepository.findByCode(code);
        if (collegeOpt.isPresent()) {
            College college = collegeOpt.get();
            populateCollegeInfo(college);
            return Optional.of(college);
        }
        return collegeOpt;
    }

    public List<College> getCollegesByName(String name) {
        List<College> colleges = collegeRepository.findByNameContaining(name);
        // 为每个学院填充关联信息
        return colleges.stream().map(this::populateCollegeInfo).collect(java.util.stream.Collectors.toList());
    }
    
    // 添加按状态获取学院的方法
    public Page<College> getCollegesByStatus(College.Status status, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<College> collegePage = collegeRepository.findByStatus(status, pageable);
        // 为每个学院填充关联信息
        return collegePage.map(this::populateCollegeInfo);
    }
    
    // 检查学院是否有关联的专业
    public boolean hasMajors(Long collegeId) {
        return majorRepository.findByCollegeId(collegeId).size() > 0;
    }
}